<%@ page import="com.macrosolution.exdebito.Perito" %>

<script type="text/javascript" >
 function remappingBean(){
 <g:if test="${peritoInstance?.fiscalAddress?.province}">
 $("#fiscal_province").select2('data',{id:${peritoInstance.fiscalAddress.province.id},text:'${peritoInstance.fiscalAddress.province.nome}'});
 </g:if>
 <g:if test="${peritoInstance?.fiscalAddress?.municipality}">
 $("#fiscal_municipality").append($('<option>', {value:"${peritoInstance.fiscalAddress.municipality.id}", text: "${peritoInstance.fiscalAddress.municipality.nome}"}));
 $("#fiscal_municipality").select2("val","${peritoInstance.fiscalAddress.municipality.id}");
 </g:if>	

	$(".select2-hidden-accessible").addClass('text-uppercase');
	$(':input').addClass('text-uppercase');
	$(':radio,:checkbox').addClass('text-uppercase');
	$('.select2-chosen').addClass('text-uppercase');
	$( ":button" ).addClass('text-uppercase');
	$('select').addClass('text-uppercase');
 }
	 
 $(document).ready(function(){
		$.validator.addMethod('numericOnlyNull', function (value) {
		       return /^[0-9]+$/.test(value) || /^$/.test(value);
		}, 'Solo Valori numerici (0-9)');
		$.validator.addMethod('CF_PIVA', function (value) {
		       return /^[a-z]{6}\d{2}[abcdehlmprst]\d{2}[a-z]\d{3}[a-z]/i.test(value) || /^[0-9]{11}$/.test(value) || /^$/.test(value);
		}, 'Inserire un codice fiscale valido o una partita iva valida');
		$.validator.addMethod('numericOnlyNull', function (value) {
		       return /^[0-9]+$/.test(value) || /^$/.test(value);
		}, 'Solo Valori numerici (0-9)');

		
		var $validator = $('#perito_form').validate({
			onfocusout:true,
			rules:{
		      surname: {
		    	  required: true
		      },
			 piva:{
				 required: true,
				 CF_PIVA:true
				},
			phone: {
		        required: false,
		        numericOnlyNull:true
		      },
		      mobile:{
		    	  required: false,
		    	  numericOnlyNull:true
			      },    	 
		      company_email: {
			        required: false,
			        email: true
			      }    	       
			},
		    messages: {
			     surname:{
				      required:"Bisogna spedificare il cognome o la denominazione sociale",
			     },
				 piva:{
					  required:"Inserire il Codice Fiscale o la Partita Iva",
					  CF_PIVA:"Inserire un Codice Fiscale o una Partita Iva corretti"
				 },		 
				 phone: {
			        numericOnly:"Il numero di telefono deve contenere solo numeri"
			     },
			     mobile:{
				        numericOnly:"Il numero di telefono deve contenere solo numeri"
			     },
				 company_email: {
			        email: "Inserire un formato email valido [info@macro-solution.it]"
			     }
		    },
		
		    highlight: function (element) {
		      $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
		    },
		    unhighlight: function (element) {
		      $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
		    },
		    errorElement: 'span',
		    errorClass: 'help-block',
		    errorPlacement: function (error, element) {
		      if (element.parent('.input-group').length) {
		        error.insertAfter(element.parent());
		      } else {
		        error.insertAfter(element);
		      }
		    },
		    invalidHandler: function(form, validator) {
				$("#validity_label").html('<i class="fa-fw fa fa-warning"></i>Ci sono '+validator.numberOfInvalids()+' infomazion'+(validator.numberOfInvalids()>1?'i':'e')+' mancant'+(validator.numberOfInvalids()>1?'i':'e'));
				$("#validity_label").show();
			}
		  });
	 
	 
	 $('#fiscal_province').select2({
	     allowClear: true,
	     minimumInputLength: 1,
	     formatNoMatches:'Nessuna corripondenza',
	     formatInputTooShort:'Inserire almeno un carattere per iniziare la ricerca',
	     formatSearching:'Stiamo cercando le province...' ,
	     ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
	         url: "/province/list",
	         dataType: 'json',
	         quietMillis: 250,
	         data: function (term, page) {
	             return {
	                 index: term, // search term
	             };
	         },
	         results: function (data, page) { // parse the results into the format expected by Select2.
	             // since we are using custom formatting functions we do not need to alter the remote JSON data
	             return { results: data.results };
	         },	         
	         cache: true
	     },  
	 }); 

 $('#fiscal_province').on("change", function(e) {
     ${remoteFunction(asynchronous: true,
            				action:'get_municipality',
							controller:'province',
							params:'\'pr=\' + this.value',
							update:'fiscal_municipality',
							onSuccess:'reset_fiscal_municipality();reset_fiscal_cap()'															
							)}   
 });
 
 $('#fiscal_municipality').select2({
  	allowClear: false,
      formatNoMatches:'Nessuna corripondenza',
      formatInputTooShort:'Inserire almeno un carattere per iniziare la ricerca',
      formatSearching:'Stiamo cercando i comuni...' ,
});

 $('#fiscal_municipality').on("change", function(e) {
     ${remoteFunction(asynchronous: true,
				action:'get_cap',
				controller:'province',
				params:'\'mun=\' + this.value',
				onSuccess:'$("#fiscal_cap").val(data)',
				)}        
	});

	remappingBean()
 });
 
function reset_fiscal_municipality(){
		$("#fiscal_municipality").select2("val","");
}
function reset_fiscal_cap(){
		$("#fiscal_cap").val("");
}
</script>
 
<div class="row col-lg-12" align="center">
<%--	<div class="form-group">--%>
<%--		<div class="input-group">--%>
<%--			<span class="input-group-addon"><i class="fa fa-user fa-lg fa-fw"></i></span>--%>
<%--			<g:textField class="form-control" name="username" value="${peritoInstance?.username}"/>--%>
<%--		</div>--%>
<%--	</div>--%>

<%--	<div class="form-group">--%>
<%--		<div class="input-group">--%>
<%--			<span class="input-group-addon"><i class="fa fa-lock fa-lg fa-fw"></i></span>--%>
<%--			<g:textField class="form-control" name="password" value="${peritoInstance?.password}"/>--%>
<%--		</div>--%>
<%--	</div>--%>

<%--	<div class="form-group">--%>
<%--		<div class="input-group">--%>
<%--			<span class="input-group-addon"><i class="fa fa-lock fa-lg fa-fw"></i></span>--%>
<%--			<g:textField class="form-control" name="originalPassword" value="${peritoInstance?.originalPassword}"/>--%>
<%--		</div>--%>
<%--	</div>--%>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-info fa-lg fa-fw"></i></span>
			<input  class=" form-control input-md" name="name" id="name" placeholder="Nome" value="${peritoInstance?.name}"/>
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-info fa-lg fa-fw"></i></span>
			<input  class=" form-control input-md" name="surname" id="surname" placeholder="Cognome / Den. Sociale *" value="${peritoInstance?.surname}"/>
		</div>
	</div>

<%--	<div class="${hasErrors(bean: peritoInstance, field: 'cf', 'error')} ">--%>
<%--		<label for="cf" class="control-label"><g:message code="perito.cf.label" default="Cf" /></label>--%>
<%--		<div>--%>
<%--			<g:textField class="form-control" name="cf" value="${peritoInstance?.cf}"/>--%>
<%--			<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'cf', 'error')}</span>--%>
<%--		</div>--%>
<%--	</div>--%>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-info fa-lg fa-fw"></i></span>
			<input  class=" form-control input-md" placeholder="Partita Iva / Cod. Fisc. *" name="piva" id="piva" value="${peritoInstance?.piva}"/>
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-phone fa-lg fa-fw"></i></span>
			<input  class=" form-control input-md" placeholder="Telefono" name="phone" id="phone" value="${peritoInstance?.phone}"/>
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-mobile-phone fa-lg fa-fw"></i></span>
			<input  class=" form-control input-md" name="mobile" id="mobile" placeholder="Cellulare" value="${peritoInstance?.mobile}"/>
		</div>
	</div>

<%--	<div class="${hasErrors(bean: peritoInstance, field: 'personal_email', 'error')} ">--%>
<%--		<label for="personal_email" class="control-label"><g:message code="perito.personal_email.label" default="Personalemail" /></label>--%>
<%--		<div>--%>
<%--			<g:textField class="form-control" name="personal_email" value="${peritoInstance?.personal_email}"/>--%>
<%--			<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'personal_email', 'error')}</span>--%>
<%--		</div>--%>
<%--	</div>--%>

			<div class="form-group">
				<div class="input-group">
					<span class="input-group-addon"><i class="fa fa-envelope fa-lg fa-fw"></i></span>
					<input  class=" form-control input-md" name="company_email" id="company_email" placeholder="Email Aziendale" value="${peritoInstance?.company_email}"/>
				</div>
			</div>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'born', 'error')} ">--%>
<%--				<label for="born" class="control-label"><g:message code="perito.born.label" default="Born" /></label>--%>
<%--				<div>--%>
<%--					<bs:datePicker name="born" precision="day"  value="${peritoInstance?.born}" default="none" noSelection="['': '']" />--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'born', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'bornProvince', 'error')} ">--%>
<%--				<label for="bornProvince" class="control-label"><g:message code="perito.bornProvince.label" default="Born Province" /></label>--%>
<%--				<div>--%>
<%--					<g:select class="form-control" id="bornProvince" name="bornProvince.id" from="${com.macrosolution.exdebito.Provincia.list()}" optionKey="id" value="${peritoInstance?.bornProvince?.id}" class="many-to-one" noSelection="['null': '']"/>--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'bornProvince', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--				<div class="form-group">--%>
<%--					<div class="input-group">--%>
<%--					<g:select class="form-control" id="fiscalAddress" name="fiscalAddress.id" from="${com.macrosolution.exdebito.Address.list()}" optionKey="id" value="${peritoInstance?.fiscalAddress?.id}" class="many-to-one" noSelection="['null': '']"/>--%>
<%--				</div>--%>
<%--			</div>--%>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>
				<input style="width:100%" data-placeholder="Provincia fiscale" class="input-md" name="fiscal_province" id="fiscal_province"/>
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>									
			<select style="width:100%" data-placeholder="Comune" class="input-md" name="fiscal_municipality" id="fiscal_municipality" >
				<option></option>
			</select>
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>
			<input class="form-control input-md" placeholder="CAP" type="text" name="fiscal_cap" 
				id="fiscal_cap" readonly="readonly" value="${peritoInstance?.fiscalAddress?.cap}">						
		</div>
	</div>

	<div class="form-group">
		<div class="input-group">
			<span class="input-group-addon"><i class="fa fa-map-marker fa-lg fa-fw"></i></span>
					<input style="width:100%" placeholder="Indirizzo Fiscale" class="form-control input-md" 
						name="fiscal_address" id="fiscal_address" value="${peritoInstance?.fiscalAddress?.address}"/>					
		</div>
	</div>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'subjects', 'error')} ">--%>
<%--				<label for="subjects" class="control-label"><g:message code="perito.subjects.label" default="Subjects" /></label>--%>
<%--				<div>					--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'subjects', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'age', 'error')} ">--%>
<%--				<label for="age" class="control-label"><g:message code="perito.age.label" default="Age" /></label>--%>
<%--				<div>--%>
<%--					<g:field class="form-control" name="age" type="number" value="${peritoInstance.age}"/>--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'age', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'owner', 'error')} ">--%>
<%--				<label for="owner" class="control-label"><g:message code="perito.owner.label" default="Owner" /></label>--%>
<%--				<div>--%>
<%--					<g:select class="form-control" id="owner" name="owner.id" from="${com.macrosolution.exdebito.User.list()}" optionKey="id" value="${peritoInstance?.owner?.id}" class="many-to-one" noSelection="['null': '']"/>--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'owner', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>
	<br><br>
	<div class="row" align="center">
		<div class="col-lg-2 col-md-offset-4">
			<div class="smart-form inline-group">
				<label class="radio">
					<input type="radio" name="enabled" class="checkbox_sex" value="1" <g:if test="${peritoInstance?.enabled || (!peritoInstance)}">checked</g:if>>
					<i></i>Attivo</label>	
			</div>
		</div>
		<div class="col-lg-2 col-md-offset-1">
			<div class="smart-form inline-group">
				<label class="radio">
					<input type="radio" name="enabled" class="checkbox_sex" value="0" <g:if test="${peritoInstance && !(peritoInstance?.enabled)}">checked</g:if>>
					<i></i>Disattivo</label>
			</div>
		</div>
	</div>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'accountExpired', 'error')} ">--%>
<%--				<label for="accountExpired" class="control-label"><g:message code="perito.accountExpired.label" default="Account Expired" /></label>--%>
<%--				<div>--%>
<%--					<bs:checkBox name="accountExpired" value="${peritoInstance?.accountExpired}" />--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'accountExpired', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'accountLocked', 'error')} ">--%>
<%--				<label for="accountLocked" class="control-label"><g:message code="perito.accountLocked.label" default="Account Locked" /></label>--%>
<%--				<div>--%>
<%--					<bs:checkBox name="accountLocked" value="${peritoInstance?.accountLocked}" />--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'accountLocked', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>

<%--			<div class="${hasErrors(bean: peritoInstance, field: 'passwordExpired', 'error')} ">--%>
<%--				<label for="passwordExpired" class="control-label"><g:message code="perito.passwordExpired.label" default="Password Expired" /></label>--%>
<%--				<div>--%>
<%--					<bs:checkBox name="passwordExpired" value="${peritoInstance?.passwordExpired}" />--%>
<%--					<span class="help-inline">${hasErrors(bean: peritoInstance, field: 'passwordExpired', 'error')}</span>--%>
<%--				</div>--%>
<%--			</div>--%>
</div>
